package com.game;


import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public final class MarsContext implements ApplicationContextAware {
    static Map<String, Object> marsBeans = new HashMap<>();

    static ApplicationContext CONTEXT;

    public static ApplicationContext getContext() {
        return CONTEXT;
    }

    public static <T> T getMarsBean(Class<T> clazz) {
        return (T)marsBeans.get(clazz.getSimpleName());
    }

    public static void addMarsBean(Object obj) {
        marsBeans.put(obj.getClass().getSimpleName(), obj);
    }

    public static Object getBean(String name) {
        return getContext().getBean(name);
    }

    public static <T> T getBean(Class<T> clazz) {
        return (T)getContext().getBean(clazz);
    }

    public static <T> T getBean(String name, Class<T> clazz) {
        return (T)getContext().getBean(name, clazz);
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        CONTEXT = applicationContext;
    }
}
